package com.clkj.xiuzheng.dao.shangcheng;

import com.clkj.xiuzheng.pojo.shangcheng.ProductComment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

import java.util.List;

@Repository
public interface ScProductcommentMapper extends Mapper<ProductComment>, MySqlMapper<ProductComment> {
    @Select("select count(*) from t_xz_productComment where product_id = #{id} and comment_status = 1")
    Integer CountCommentById(Integer id);

    @Select({"<script>",
             "SELECT c.product_comment_id,c.is_top,c.comment_user_id,c.comment_status,c.company_id,c.comment_type, c.comment_date,c.comment_content,p.product_name,u.user_name FROM ",
                        "t_xz_productComment c LEFT JOIN t_xz_product p on c.product_id=p.product_id LEFT JOIN t_xz_user u on u.user_id = c.comment_user_id where",
                        "1=1",
                        "<if test = 'productId!=null'>",
                        "and p.product_id = #{productId}",
                        "</if>",
                        "<if test = 'companyId!=null'>",
                        "and c.company_id = #{companyId}",
                        "</if>",
            "</script>"
            })
    List<ProductComment> selectCommentByCompanyId(@Param("companyId") Integer companyId , @Param("productId") Integer productId);


    @Select("select top 3 a.*,b.user_name,b.user_img from t_xz_productComment a " +
            "left join t_xz_user b  on a.comment_user_id = b.user_id " +
            "where a.product_id = #{productId} order by a.is_top desc ")
    List<ProductComment> listTopCommentByProductId(@Param("productId")Integer productId);

    @Insert("insert into t_xz_productComment (comment_type,comment_content,comment_user_id,comment_date,comment_status,product_id,company_id,is_top) values (#{commentType},#{commentContent},#{commentUserId},#{commentDate},1,#{productId},#{companyId},0)")
    @Options(useGeneratedKeys = true, keyProperty = "productCommentId", keyColumn = "product_comment_id")
    void addComment(ProductComment productComment);

    @Select("select c.* , u.user_name,u.user_img from t_xz_productComment c left join t_xz_user u on c.comment_user_id = u.user_id where c.product_id=#{productId} and c.comment_type = #{commentType} order by c.comment_date desc ")
    List<ProductComment> getProductByType(@Param("productId") Integer productId, @Param("commentType") Integer commentType);
    @Select("select c.* , u.user_name from t_xz_productComment c left join t_xz_user u on c.comment_user_id = u.user_id where c.product_id=#{productId}  order by c.comment_date desc ")
    List<ProductComment> getProductById(@Param("productId") Integer productId);
}
